﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;

using Factonomy.AzureUtility.Core;
using Factonomy.AzureUtility.Core.Storage;
using Factonomy.AzureUtility.Core.StorageHelpers;

namespace Factonomy.AzureUtility.Controls
{
    /// <summary>
    /// Interaction logic for ViewBlobControl.xaml
    /// </summary>
    public partial class ViewBlobControl : UserControl
    {
        public event BlobPropertiesDelegate SelectedIndexChanged;

        private ConfigAccount _account = null;
        private BlobContainer _container = null;
        private ContainerProperties _containerProperties = null;
        private BlobContextMenu _blobContextMenu = new BlobContextMenu();

        protected virtual void OnSelectedIndexChanged(BlobContainer container, BlobProperties properties)
        {
            if (SelectedIndexChanged != null)
                SelectedIndexChanged(container, properties);
        }

        public ViewBlobControl()
        {
            InitializeComponent();
            BlobHelper.Instance.BlobListChanged += new NoParameterDelegate(LoadBlobs);
            BlobHelper.Instance.BlobDeleted += new NoParameterDelegate(LoadBlobs);
            this.uiListViewMain.PreviewMouseRightButtonDown += new MouseButtonEventHandler(uiListViewMain_PreviewMouseRightButtonDown);
        }

        void uiListViewMain_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
        }

        public void LoadContainer(BlobContainer container)
        {
            this._container = container;
            this._account = Config.Instance.GetAccount(this._container.AccountName);
            this._containerProperties = this._container.GetContainerProperties();
            this._blobContextMenu.LoadContainer(this._container);
            this.uiListViewMain.SelectionChanged += new SelectionChangedEventHandler(OnListViewSelectedChanged);
            this.uiListViewMain.ContextMenu = this._blobContextMenu;
        }

        void OnListViewSelectedChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView listView = sender as ListView;
            if (listView.SelectedItem == null)
            {

            }
            else
            {
                if (listView.SelectedItem.GetType() == typeof(BlobProperties))
                {
                    BlobProperties properties = listView.SelectedItem as BlobProperties;
                    this.OnSelectedIndexChanged(this._container, properties);
                }
            }
        }

        public void LoadBlobs()
        {
            this.ListBlobs(true);
            this.ChangeColumns();            
        }

        private void ChangeColumns()
        {
            if (this.Dispatcher.CheckAccess())
            {
                double columnLengthTotal = 10;
                for (int i = 1; i < this.uiGridView.Columns.Count; i++)
                {
                    columnLengthTotal += this.uiGridView.Columns[i].ActualWidth;
                }

                this.uiGridView.Columns[0].Width = this.ActualWidth - columnLengthTotal;
            }
            else
            {
                this.Dispatcher.Invoke(DispatcherPriority.Normal, new NoParameterDelegate(this.ChangeColumns));
            }
        }

        private void ListBlobs(object sender)
        {
            if (bool.Parse(sender.ToString()))
            {
                Thread thread = new Thread(new ParameterizedThreadStart(this.ListBlobs));
                thread.IsBackground = true;
                thread.Start(false);
            }
            else
            {
                Thread.Sleep(1);
                IEnumerable<object> blobs = BlobHelper.Instance.ListBlobs(this._account, this._container.ContainerName);
                this.UpdateBlobList(blobs);
            }
        }

        private void UpdateBlobList(object blobs)
        {
            if (this.uiListViewMain.Dispatcher.CheckAccess())
            {
                this.uiListViewMain.ItemsSource = blobs as IEnumerable<object>;
                BlobHelper.Instance.OnLoadBlobListComplete();
            }
            else
            {
                this.uiListViewMain.Dispatcher.Invoke(DispatcherPriority.Normal, new ParameterDelegate(this.UpdateBlobList), blobs);
            }
        }

    }
    
    
}
